package start;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class HibernateSpitterDao implements SpitterDAO {
    private SessionFactory sessionFactory;
    public HibernateSpitterDao() {
        System.out.println("HibernateSpitterDao creat..");        
    }
    
    @Autowired
    public HibernateSpitterDao(SessionFactory sessionFactory) {
        System.out.println("HibernateSpitterDao creat("+sessionFactory+")..");
        this.sessionFactory = sessionFactory;
    }

    public Session currentSession() {
        System.out.println("currentSession()"); 
        return sessionFactory.openSession();
    }

    @Override
    public void addSpitter(Spitter spitter) {
        currentSession().save(spitter);
    }

    @Override
    public void addSpitterNamedParam(Spitter spitter) {
    }

    @Override
    public Spitter getSpitterById(long id) {
        return (Spitter)currentSession().get(Spitter.class, id);
    }

    public void saveSpitter(Spitter spitter) {
        currentSession().update(spitter);
    }
}
